package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesGyInfoDTO;
import com.anolesoft.twelvePlan.dto.MesGyInfoExport;
import com.anolesoft.twelvePlan.dto.MesGyInfoToPlanDTO;
import com.anolesoft.twelvePlan.dto.MesHistoryFabinfoDTO;
import com.anolesoft.twelvePlan.entity.MesGyInfoDO;
import com.anolesoft.twelvePlan.entity.MesHistoryFabinfoDO;
import com.anolesoft.twelvePlan.mapper.MesGyInfoMapper;
import com.anolesoft.twelvePlan.mapper.MesPlanXiaDaMapper;
import com.anolesoft.twelvePlan.service.MesGyInfoService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/3/21
 */
@Service
@Slf4j
public class MesGyInfoServiceImpl implements MesGyInfoService {
    @Autowired
    private MesGyInfoMapper mesGyInfoMapper;
    @Autowired
    private MesPlanXiaDaMapper mesPlanXiaDaMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageData<List<MesGyInfoDTO>> queryGyInfo(MesGyInfoDTO mesGyInfoDTO, Integer pageNo, Integer pageSize) {
        Page<MesGyInfoDO> page = new Page<>(pageNo, pageSize);
        // 取消分页器sql优化 当sql里left join子句的表中没有字段出现在where子句中时left join子句会被整个优化掉，导致分页器总行数total数量异常，默认为true
        page.setOptimizeCountSql(false);
        List<MesGyInfoDO> mesGyInfoDOList = mesGyInfoMapper.queryGyInfo(MesGyInfoDO.builder()
                .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                .partname(CommonUtils.stringNull(mesGyInfoDTO.getPartname()))
                .jmjcategory(CommonUtils.stringNull(mesGyInfoDTO.getJmjcategory()))
                .longperiodpart(CommonUtils.stringNull(mesGyInfoDTO.getLongperiodpart()))
                .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                .build(), page);
        List<MesGyInfoDTO> mesGyInfoDTOList = mesGyInfoDOList.stream()
                .map(t -> BeanUtil.copyProperties(t, MesGyInfoDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesGyInfoDTOList);
    }

    @Override
    public int insertGyInfo(MesGyInfoDTO mesGyInfoDTO) {
        int repeat = mesGyInfoMapper.repeatGyInfo(MesGyInfoDO.builder()
                .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                .parentpartid(CommonUtils.stringNull(mesGyInfoDTO.getParentpartid()))
                .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                .jsworkshop(CommonUtils.stringNull(mesGyInfoDTO.getJsworkshop()))
                .deliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getDeliveryorder()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = mesGyInfoMapper.insertGyInfo(MesGyInfoDO.builder()
                    .deliverytype(CommonUtils.stringNull(mesGyInfoDTO.getDeliverytype()))
                    .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                    .parentpartid(CommonUtils.stringNull(mesGyInfoDTO.getParentpartid()))
                    .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                    .djnum(mesGyInfoDTO.getDjnum())
                    .innerwork(CommonUtils.stringNull(mesGyInfoDTO.getInnerwork()))
                    .processroute(CommonUtils.stringNull(mesGyInfoDTO.getProcessroute()))
                    .parttype(CommonUtils.stringNull(mesGyInfoDTO.getParttype()))
                    .jsworkshop(CommonUtils.stringNull(mesGyInfoDTO.getJsworkshop()))
                    .deliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getDeliveryorder()))
                    .llworkshop(CommonUtils.stringNull(mesGyInfoDTO.getLlworkshop()))
                    .lldeliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getLldeliveryorder()))
                    .partname(CommonUtils.stringNull(mesGyInfoDTO.getPartname()))
                    .danzhuangnum(mesGyInfoDTO.getDanzhuangnum())
                    .longperiodpart(CommonUtils.stringNull(mesGyInfoDTO.getLongperiodpart()))
                    .jmjcategory(CommonUtils.stringNull(mesGyInfoDTO.getJmjcategory()))
                    .productfamily(CommonUtils.stringNull(mesGyInfoDTO.getProductfamily()))
                    .processorsnew(CommonUtils.stringNull(mesGyInfoDTO.getProcessorsnew()))
                    .junjian(CommonUtils.stringNull(mesGyInfoDTO.getJunjian()))
                    .materialbrand(CommonUtils.stringNull(mesGyInfoDTO.getMaterialbrand()))
                    .materialbrandstate(CommonUtils.stringNull(mesGyInfoDTO.getMaterialbrandstate()))
                    .materialspecification(CommonUtils.stringNull(mesGyInfoDTO.getMaterialspecification()))
                    .length(CommonUtils.stringNull(mesGyInfoDTO.getLength()))
                    .width(CommonUtils.stringNull(mesGyInfoDTO.getWidth()))
                    .height(CommonUtils.stringNull(mesGyInfoDTO.getHeight()))
                    .forgedcastingscode(CommonUtils.stringNull(mesGyInfoDTO.getForgedcastingscode()))
                    .partproperty(CommonUtils.stringNull(mesGyInfoDTO.getPartproperty()))
                    .heattreattype(CommonUtils.stringNull(mesGyInfoDTO.getHeattreattype()))
                    .bfcl(CommonUtils.stringNull(mesGyInfoDTO.getBfcl()))
                    .painting(CommonUtils.stringNull(mesGyInfoDTO.getPainting()))
                    .wash(CommonUtils.stringNull(mesGyInfoDTO.getWash()))
                    .specialinspect(CommonUtils.stringNull(mesGyInfoDTO.getSpecialinspect()))
                    .sandblasting(CommonUtils.stringNull(mesGyInfoDTO.getSandblasting()))
                    .relievestress(CommonUtils.stringNull(mesGyInfoDTO.getRelievestress()))
                    .electropolishing(mesGyInfoDTO.getElectropolishing())
                    .others(CommonUtils.stringNull(mesGyInfoDTO.getOthers()))
                    .weight(mesGyInfoDTO.getWeight())
                    .remarks(CommonUtils.stringNull(mesGyInfoDTO.getRemarks()))
                    .createuid(CommonUtils.stringNull(mesGyInfoDTO.getCreateuid()))
                    .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                    .build());
        }
        return count;
    }

    @Override
    public int updateGyInfo(MesGyInfoDTO mesGyInfoDTO) {
        int repeat = mesGyInfoMapper.repeatGyInfo(MesGyInfoDO.builder()
                .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                .parentpartid(CommonUtils.stringNull(mesGyInfoDTO.getParentpartid()))
                .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                .jsworkshop(CommonUtils.stringNull(mesGyInfoDTO.getJsworkshop()))
                .deliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getDeliveryorder()))
                .objuid(CommonUtils.stringNull(mesGyInfoDTO.getObjuid()))
                .build());
        log.debug("查重结果：" + repeat);
        int count = 0;
        if (repeat == 0) {
            count = mesGyInfoMapper.updateGyInfo(MesGyInfoDO.builder()
                    .objuid(CommonUtils.stringNull(mesGyInfoDTO.getObjuid()))
                    .deliverytype(CommonUtils.stringNull(mesGyInfoDTO.getDeliverytype()))
                    .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                    .parentpartid(CommonUtils.stringNull(mesGyInfoDTO.getParentpartid()))
                    .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                    .djnum(mesGyInfoDTO.getDjnum())
                    .innerwork(CommonUtils.stringNull(mesGyInfoDTO.getInnerwork()))
                    .processroute(CommonUtils.stringNull(mesGyInfoDTO.getProcessroute()))
                    .parttype(CommonUtils.stringNull(mesGyInfoDTO.getParttype()))
                    .jsworkshop(CommonUtils.stringNull(mesGyInfoDTO.getJsworkshop()))
                    .deliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getDeliveryorder()))
                    .llworkshop(CommonUtils.stringNull(mesGyInfoDTO.getLlworkshop()))
                    .lldeliveryorder(CommonUtils.stringNull(mesGyInfoDTO.getLldeliveryorder()))
                    .partname(CommonUtils.stringNull(mesGyInfoDTO.getPartname()))
                    .danzhuangnum(mesGyInfoDTO.getDanzhuangnum())
                    .longperiodpart(CommonUtils.stringNull(mesGyInfoDTO.getLongperiodpart()))
                    .jmjcategory(CommonUtils.stringNull(mesGyInfoDTO.getJmjcategory()))
                    .productfamily(CommonUtils.stringNull(mesGyInfoDTO.getProductfamily()))
                    .processorsnew(CommonUtils.stringNull(mesGyInfoDTO.getProcessorsnew()))
                    .junjian(CommonUtils.stringNull(mesGyInfoDTO.getJunjian()))
                    .materialbrand(CommonUtils.stringNull(mesGyInfoDTO.getMaterialbrand()))
                    .materialbrandstate(CommonUtils.stringNull(mesGyInfoDTO.getMaterialbrandstate()))
                    .materialspecification(CommonUtils.stringNull(mesGyInfoDTO.getMaterialspecification()))
                    .length(CommonUtils.stringNull(mesGyInfoDTO.getLength()))
                    .width(CommonUtils.stringNull(mesGyInfoDTO.getWidth()))
                    .height(CommonUtils.stringNull(mesGyInfoDTO.getHeight()))
                    .forgedcastingscode(CommonUtils.stringNull(mesGyInfoDTO.getForgedcastingscode()))
                    .partproperty(CommonUtils.stringNull(mesGyInfoDTO.getPartproperty()))
                    .heattreattype(CommonUtils.stringNull(mesGyInfoDTO.getHeattreattype()))
                    .bfcl(CommonUtils.stringNull(mesGyInfoDTO.getBfcl()))
                    .painting(CommonUtils.stringNull(mesGyInfoDTO.getPainting()))
                    .wash(CommonUtils.stringNull(mesGyInfoDTO.getWash()))
                    .specialinspect(CommonUtils.stringNull(mesGyInfoDTO.getSpecialinspect()))
                    .sandblasting(CommonUtils.stringNull(mesGyInfoDTO.getSandblasting()))
                    .relievestress(CommonUtils.stringNull(mesGyInfoDTO.getRelievestress()))
                    .electropolishing(mesGyInfoDTO.getElectropolishing())
                    .others(CommonUtils.stringNull(mesGyInfoDTO.getOthers()))
                    .weight(mesGyInfoDTO.getWeight())
                    .remarks(CommonUtils.stringNull(mesGyInfoDTO.getRemarks()))
                    .modifyuid(CommonUtils.stringNull(mesGyInfoDTO.getModifyuid()))
                    .build());
            mesPlanXiaDaMapper.updateFlag(mesGyInfoDTO.getObjuid());
        }
        return count;
    }

    @Override
    public int deleteGyInfo(MesGyInfoDTO mesGyInfoDTO) {
        int count = mesGyInfoMapper.deleteGyInfo(MesGyInfoDO.builder()
                .objuid(CommonUtils.stringNull(mesGyInfoDTO.getObjuid()))
                .deleteuid(CommonUtils.stringNull(mesGyInfoDTO.getDeleteuid()))
                .build());
        mesPlanXiaDaMapper.deleteFlag(mesGyInfoDTO.getObjuid());
        return count;
    }

    @Override
    public int exportGyInfo(MesGyInfoDTO mesGyInfoDTO) {
        int count = mesGyInfoMapper.exportGyInfo(MesGyInfoDO.builder()
                .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                .partname(CommonUtils.stringNull(mesGyInfoDTO.getPartname()))
                .jmjcategory(CommonUtils.stringNull(mesGyInfoDTO.getJmjcategory()))
                .longperiodpart(CommonUtils.stringNull(mesGyInfoDTO.getLongperiodpart()))
                .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                .build());
        return count;
    }

    @Override
    public void uploadGyInfo(List<MesGyInfoDO> list) {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        MesGyInfoMapper mesGyInfoMapper = sqlSession.getMapper(MesGyInfoMapper.class);
        list.forEach(a -> {
            mesGyInfoMapper.insert(a);
        });
        sqlSession.commit();
    }

    @Override
    public int deleteSelect(MesGyInfoDTO mesGyInfoDTO) {
        int count = mesGyInfoMapper.deleteSelect(MesGyInfoDO.builder()
                .jx(CommonUtils.stringNull(mesGyInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesGyInfoDTO.getPartid()))
                .partname(CommonUtils.stringNull(mesGyInfoDTO.getPartname()))
                .jmjcategory(CommonUtils.stringNull(mesGyInfoDTO.getJmjcategory()))
                .longperiodpart(CommonUtils.stringNull(mesGyInfoDTO.getLongperiodpart()))
                .factoryId(CommonUtils.stringNull(mesGyInfoDTO.getFactoryId()))
                .deleteuid(CommonUtils.stringNull(mesGyInfoDTO.getDeleteuid()))
                .build());
        return count;
    }

}
